package com.dbq.fast.business.product.ui;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

import com.dbq.fast.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 */

public class PayChooseItem extends LinearLayout {

    public static int ALI_PAY = 1;
    public static int WX_PAY = 2;
    public static int GOOGLE_PAY = 3;
    public static int STRIPE_PAY = 4;
    public static int WFEE_PAY = 5;

    private PayTypeItemView p1;

    @BindView(R.id.root_ll)
    LinearLayout rootLl;

    private OnChooseStatusListener listener;
    private boolean isAllowPay = true;
    private boolean isSupportWx = true;
    private OnSupportWxPayListener supportWxPayListener;

    public PayChooseItem(Context context) {
        this(context, null);
    }

    public PayChooseItem(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PayChooseItem(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
        addItem();
        initListener();
    }

    private void initView() {
        View rooView = LayoutInflater.from(getContext()).inflate(R.layout.view_pay_choose_item, null);
        ButterKnife.bind(this, rooView);
        this.addView(rooView);
    }

    private void addItem() {
        rootLl.setOrientation(LinearLayout.VERTICAL);

        p1 = new PayTypeItemView(this.getContext());
        p1.setType(R.drawable.ic_alipay, R.string.aliPay, true);
        p1.setStatus(0);
        p1.setPayType(ALI_PAY);
        rootLl.addView(p1);
//
//        PayTypeItemView p2 = new PayTypeItemView(this.getContext());
//        p2.setStatus(0);
//        p2.setType(R.drawable.ic_credit_card, R.string.credit_card, false);
//        p2.setIsVisible(true);
//        p2.setStripeInfoTv(getResources().getString(R.string.credit_card_hint));
//        p2.setPayType(STRIPE_PAY);
//        rootLl.addView(p2);


//        PayTypeItemView p3 = new PayTypeItemView(this.getContext());
//        p3.setType(R.drawable.ic_wfee, R.string.wfee_pay, false);
//        p3.setStatus(0);
//        p3.setPayType(WFEE_PAY);
//        rootLl.addView(p3);
    }


    private void initListener() {
        for (int i = 0; i < rootLl.getChildCount(); i++) {
            View v = rootLl.getChildAt(i);
            if (v instanceof PayTypeItemView) {
                v.setOnClickListener(new ItemClick());
            }
        }
    }


    private class ItemClick implements OnClickListener {

        public ItemClick() {
        }

        @Override
        public void onClick(View v) {
            if (v instanceof PayTypeItemView) {
                for (int i = 0; i < rootLl.getChildCount(); i++) {
                    View child = rootLl.getChildAt(i);
                    PayTypeItemView v0 = ((PayTypeItemView) child);
                    if (v0 == v) {
                        v0.setPayStatus(true);
                        selectPayType(v0.getPayType());
                    } else {
                        v0.setPayStatus(false);
                    }
                    Log.i("tag", "" + v0.payName);
                }

            }
        }
    }

    public void selectPayType(int type) {
        if (!isAllowPay) return;
        if (type == ALI_PAY) {
            if (listener != null) {
                listener.onChooseItem(ALI_PAY);
            }

        } else if (type == WX_PAY) {
            if (isSupportWx) {
                if (listener != null) {
                    listener.onChooseItem(WX_PAY);
                }
            } else {
                if (supportWxPayListener != null) {
                    supportWxPayListener.hint();
                }
            }
        } else if (type == GOOGLE_PAY) {
            if (listener != null) {
                listener.onChooseItem(GOOGLE_PAY);
            }
        } else if (type == STRIPE_PAY) {
            if (listener != null) {
                listener.onChooseItem(STRIPE_PAY);
            }
        } else if (type == WFEE_PAY) {
            if (listener != null) {
                listener.onChooseItem(WFEE_PAY);
            }
        }
    }


    public void setOnSelectItemListener(OnChooseStatusListener listener) {
        this.listener = listener;
    }

    public interface OnChooseStatusListener {
        void onChooseItem(int id);
    }


    public void setIsAllowPay(boolean isCanPay) {
        this.isAllowPay = isCanPay;
        for (int i = 0; i < rootLl.getChildCount(); i++) {
            View v = rootLl.getChildAt(i);
            if (v instanceof PayTypeItemView) {
                PayTypeItemView v0 = (PayTypeItemView) v;
                v0.payStatus.setVisibility(isCanPay ? View.VISIBLE : View.INVISIBLE);
            }
        }
    }

    public void setOnSupportWxPayListener(OnSupportWxPayListener supportWxPayListener) {
        this.supportWxPayListener = supportWxPayListener;
    }

    public interface OnSupportWxPayListener {
        void hint();
    }

    /**
     * 根据是否安装微信，显示是否支持微信支付选项
     * @param isSupportWx
     */
    public void isSupportWx(boolean isSupportWx) {
        this.isSupportWx = isSupportWx;
    }
}
